package com.luck.emp.ass.web;


import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.ass.AssVo;
import com.luck.emp.ass.logic.IAssLogic;
import com.luck.emp.ass.qry.AssQry;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.entity.EmpTWhAssD;
import com.luck.emp.entity.EmpTWhAssM;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class AssAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IAssLogic assLogic;
		IWhLogic whLogic;
		/**
		 * 查询对象
		 */
		AssQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTWhAssM entity;
		List<EmpTWarehouse> whList;
		String whCode;
		Long assAmount;//可组装或拆卸的数量
		List<AssVo> vos;
		List<EmpTWhAssD> assd;
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (AssQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new AssQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = assLogic.findAssPage(qry);
			return "list";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(entity == null)
				entity = new EmpTWhAssM();
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userInfo.getUser());
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			super.event="ADD";
			if(entity.getBillType().equals("16"))
				super.title="新增组装单";
			else
				super.title="新增拆卸单";
			return "edit";
		}
		/**
		 * 修改组装单
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			//新增操作
			entity.setAssCode(null);
			//entity.setBillType("16");
			assLogic.saveAss(userInfo, entity);
			//super.addAttribute("qry.pk", entity.getAssCode());
			super.redirectUrl = "/emp/ass_list.action";
			return super.GLOBAL_SUCCESS;
		}

		/**
		 * 根据商品编码和仓库，查找最大的可组装数
		 * @return
		 * @throws Exception
		 */
		public String maxAssAmount() throws Exception{
			assAmount = assLogic.findAssAmount(entity.getProductCode(), whCode);
			super.rendText(assAmount.toString());
			return null;
		}
		/**
		 * 根据整件商品和数量查询库存情况 
		 */	
		public String findAssProductList(){
			vos = assLogic.findAssVos(entity.getProductCode(), whCode);
			super.rendText(JSONArray.fromObject(vos).toString());
			return null;
		}
		public String view() throws Exception{
			entity = assLogic.get(EmpTWhAssM.class, entity.getAssCode());
			assd = assLogic.findAssd(entity.getAssCode());
			return "view";
		}
		public String print() throws Exception{
			entity = assLogic.get(EmpTWhAssM.class, entity.getAssCode());
			assd = assLogic.findAssd(entity.getAssCode());
			return "print";
		}
		public AssQry getQry(){
			return this.qry;
		}
		public void setQry(AssQry qry){
			this.qry = qry;
		}
		
		public IAssLogic getAssLogic(){
			return this.assLogic;
		}
		public void setAssLogic(IAssLogic assLogic){
			this.assLogic = assLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTWhAssM getEntity(){
			return entity;
		}
		public void setEntity(EmpTWhAssM entity){
			this.entity = entity;
		}

		public List<EmpTWarehouse> getWhList() {
			return whList;
		}
		public void setWhList(List<EmpTWarehouse> whList) {
			this.whList = whList;
		}
		public String getWhCode() {
			return whCode;
		}
		public void setWhCode(String whCode) {
			this.whCode = whCode;
		}
		public Long getAssAmount() {
			return assAmount;
		}
		public void setAssAmount(Long assAmount) {
			this.assAmount = assAmount;
		}
		public IWhLogic getWhLogic() {
			return whLogic;
		}
		public void setWhLogic(IWhLogic whLogic) {
			this.whLogic = whLogic;
		}
		public List<AssVo> getVos() {
			return vos;
		}
		public void setVos(List<AssVo> vos) {
			this.vos = vos;
		}
		public List<EmpTWhAssD> getAssd() {
			return assd;
		}
		public void setAssd(List<EmpTWhAssD> assd) {
			this.assd = assd;
		}
		
		
}